import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'


import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [VantResolver()],
    }),
    Components({
      resolvers: [VantResolver()],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    host: "0.0.0.0",
    port: 7777,

    // 配置反向代理
    proxy: {

      /* 
         反向代理非常好用  => 仅限于开发环境(开发环境有本地服务器)  => 项目打包之后就变成了静态文件(html + css + js) ,没有脚手架的服务器环境
      
      
      */

      // /api 代理地址  => 凡是以 http:localhost:7777/api  地址开头的请求 都要做代理操作
      "/api": { // 开发环境
        target: "http://47.109.189.52:5566/api", // 目标地址(开发环境)  => 代理服务器需要向该地址发送请求
        changeOrigin: true,  // 是否跨域,
        rewrite: (path) => {  //  //路径重写，把'/api'替换为''

          /* 
            /api/banner/list   => 如果不替换 http://47.109.189.52:5566/api + /api/banner/list   => 有问题
            console.log(path,"http://47.109.189.52:5566/api" + path);
            */

          /* 
          /api/banner/list   => 替换/api后   =>  /banner/list   
          http://47.109.189.52:5566/api + /banner/list    
          console.log(path,"http://47.109.189.52:5566/api" + path);
          */
          console.log(path, "http://47.109.189.52:5566/api" + path.replace(/^\/api/, ""));
          return path.replace(/^\/api/, "");
        }
      },

      // 测试环境
      "/test-api": { 
        target: "http://47.109.189.52:5566/api", // 目标地址( 测试环境)  => 代理服务器需要向该地址发送请求
        changeOrigin: true,  // 是否跨域,
        rewrite: (path) => {  //  //路径重写，把'/api'替换为''

          /* 
            /api/banner/list   => 如果不替换 http://47.109.189.52:5566/api + /api/banner/list   => 有问题
            console.log(path,"http://47.109.189.52:5566/api" + path);
            */

          /* 
          /api/banner/list   => 替换/api后   =>  /banner/list   
          http://47.109.189.52:5566/api + /banner/list    
          console.log(path,"http://47.109.189.52:5566/api" + path);
          */
          console.log(path, "http://47.109.189.52:5566/api" + path.replace(/^\/api/, ""));
          return path.replace(/^\/api/, "");
        }
      },

      // 生成环境
      "/prod-api": {
        target: "http://47.109.189.52:5566/api", // 目标地址(生成环境)  => 代理服务器需要向该地址发送请求
        changeOrigin: true,  // 是否跨域,
        rewrite: (path) => {  //  //路径重写，把'/api'替换为''

          /* 
            /api/banner/list   => 如果不替换 http://47.109.189.52:5566/api + /api/banner/list   => 有问题
            console.log(path,"http://47.109.189.52:5566/api" + path);
            */

          /* 
          /api/banner/list   => 替换/api后   =>  /banner/list   
          http://47.109.189.52:5566/api + /banner/list    
          console.log(path,"http://47.109.189.52:5566/api" + path);
          */
          console.log(path, "http://47.109.189.52:5566/api" + path.replace(/^\/api/, ""));
          return path.replace(/^\/api/, "");
        }
      },


    }
  }
})
